home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 June / EnigmA AMIGA RUN 08 (1996)(G.R. Edizioni)(IT)[!][issue 1996-06][EARSAN CD VII].iso / earcd / dbase / fiasco.lha / Fiasco_1.2 / ARexx / graphprint.rexx < prev    next >
OS/2 REXX Batch file  |  1996-04-29  |  10KB  |  524 lines

  1. /* GraphPrint.rexx
  2.  * Render a TeX diagram of Fiasco data
  3.  * Requires rexxsupport.library and
  4.  * rexxmathlib.library by Willy Langeveld
  5.  * Created documents require LaTeX, epic, eepic and specialhost running
  6.  * Copyright © 1996 Nils Bandener
  7.  * $VER: GraphPrint.rexx 4.13 (29.4.96)
  8.  */
  9.  
  10. /* To Do:
  11.  *
  12.  * Support for several graphs in one diagram
  13.  * Configure the marks for points
  14.  * Configure use of TeX program (bigtex/virtex)
  15.  *
  16.  */
  17.  
  18. Options Results
  19. Address FIASCO
  20.  
  21. Signal on Syntax
  22. Signal on Halt
  23. Signal on Break_C
  24. Signal on Failure
  25.  
  26. F_LockGUI
  27.  
  28. /*
  29.  *  Get fields for graphic
  30.  */
  31.  
  32. F_RequestField '"Please select a field*nfor the x axis"'
  33. if rc ~= 0 then call bailout
  34. xname = result
  35.  
  36. F_RequestField '"Please select a field*nfor the y axis"'
  37. if rc ~= 0 then call bailout
  38. yname = result
  39.  
  40. F_RequestChoice '"Do you want to save the result in a TeX file*nor compile it temporarily for viewing or printing?" "Save to file|View/Print"'
  41. desttype = result
  42.  
  43. if desttype = 1 then do
  44.     F_RequestFile '"Ram:FiascoOutput.tex" Pattern "#?.tex" Title "Select a TeX file" Savemode'
  45.     if rc ~= 0 then call bailout
  46.     texfile = result
  47.  
  48.     F_RequestChoice '"Do you want a complete file*nor a file without headers usable for \input?" "Complete|Without headers"'
  49.     completefile = result
  50.  
  51. end
  52. else do
  53.     texfile = "t:FiascoOutput.tex"
  54.     completefile = 1
  55. end
  56.  
  57. /*
  58.  * Define a symbol for value positions
  59.  */
  60.  
  61. cross = "\makebox(0,0){$\otimes$}"
  62.  
  63. /*
  64.  * Open libraries
  65.  */
  66.  
  67. if ~show(libraries, "rexxsupport.library") then do
  68.  
  69.     if ~addlib("rexxsupport.library", 0, -30, 0) then do
  70.  
  71.         F_RequestChoice '"Could not open*nrexxsupport.library"' '"Cancel"'
  72.  
  73.         call bailout
  74.  
  75.     end
  76. end
  77.  
  78. if ~show(libraries, "rexxmathlib.library") then do
  79.  
  80.     if ~addlib("rexxmathlib.library", 0, -30, 0) then do
  81.  
  82.         F_RequestChoice '"Could not open*nrexxmathlib.library"' '"Cancel"'
  83.  
  84.         call bailout
  85.  
  86.     end
  87. end
  88.  
  89. F_CountRecs
  90. num = result
  91.  
  92. /*
  93.  *  Search for extrema
  94.  */
  95.  
  96. F_SetStatus '"extrema..."'
  97.  
  98. F_GetFieldCont xname Record 1
  99. maxx = minx = Result
  100.  
  101. F_GetFieldCont yname Record 1
  102. maxy = miny = Result
  103.  
  104. do i = 1 to num
  105.  
  106.     F_Progress i num
  107.  
  108.     F_GetFieldCont xname Record i
  109.     if result > maxx then maxx = result
  110.     if result < minx then minx = result
  111.  
  112.     F_GetFieldCont yname Record i
  113.     if result > maxy then maxy = result
  114.     if result < miny then miny = result
  115.  
  116. end
  117.  
  118. /*
  119.  *  Dimensions of TeX graphic
  120.  */
  121.  
  122. w = 320
  123. h = 350
  124.  
  125. /*
  126.  *  Values for scale
  127.  */
  128.  
  129. scalebasex = 5
  130. scalebasey = 5
  131.  
  132. /*
  133.  *  Origin?
  134.  */
  135.  
  136. orgoo = 1   /* (0;0) */
  137.  
  138. /*
  139.  *  Advanced options menu
  140.  */
  141.  
  142. done = 0
  143.  
  144. do while ~done
  145.  
  146.     F_RequestChoice '"Advanced Options Menu:" "Continue|Edit Scale Base|Edit Origin|Cancel"'
  147.  
  148.     if result = 1 then do
  149.  
  150.         done = 1
  151.  
  152.     end
  153.     else if result = 2 then do
  154.  
  155.         F_RequestNumber scalebasex 'Text "Please input scale base for x axis"'
  156.         if rc = 0 then scalebasex = result
  157.  
  158.         F_RequestNumber scalebasey 'Text "Please input scale base for y axis"'
  159.         if rc = 0 then scalebasey = result
  160.  
  161.     end
  162.     else if result = 3 then do
  163.  
  164.         F_RequestChoice '"What origin do you want to use?"' '"(0;0)|project specific"'
  165.         if result = 1 then orgoo = 1
  166.         else orgoo = 0
  167.  
  168.     end
  169.     else do
  170.  
  171.         call bailout
  172.  
  173.     end
  174.  
  175. end
  176.  
  177. if orgoo = 1 then do
  178.  
  179.     if minx > 0 then minx = 0
  180.     if miny > 0 then miny = 0
  181.  
  182. end
  183.  
  184. /*
  185.  *  Calculate the space that the graphic requires
  186.  */
  187.  
  188. realw = abs(maxx - minx)
  189. realh = abs(maxy - miny)
  190.  
  191. /*
  192.  * Calculate values which can be used to
  193.  * convert the "real world" values to
  194.  * TeX conform values
  195.  */
  196.  
  197. xfact = w / (realw) * 0.95
  198. yfact = h / (realh) * 0.95
  199.  
  200. xabs = -(minx * xfact);
  201. yabs = -(miny * yfact);
  202.  
  203. /*
  204.  * Look whether the axes may have a zero position
  205.  */
  206.  
  207. xaxisnull = maxx >= 0 & minx <= 0
  208. yaxisnull = maxy >= 0 & miny <= 0
  209.  
  210. if yaxisnull | orgoo then
  211.     xaxisy = 0
  212. else
  213.     xaxisy = miny
  214.  
  215. if xaxisnull | orgoo then
  216.     yaxisx = 0
  217. else
  218.     yaxisx = minx
  219.  
  220. F_SetStatus '"Creating file..."'
  221.  
  222. /*
  223.  * Open file
  224.  */
  225.  
  226. if ~open("file", texfile, "write") then do
  227.  
  228.     F_RequestChoice '"Could not open*n' || texfile || '"' '"Cancel"'
  229.  
  230.     call bailout
  231.  
  232. end
  233.  
  234. /*
  235.  * write file header
  236.  */
  237.  
  238. if completefile = 1 then
  239.     call writeln("file", "\documentstyle[epic,eepic]{article} \begin{document}")
  240.  
  241. call writeln("file", "% This document has been created by Fiasco Release 1.2")
  242. call writeln("file", "% and GraphPrint.rexx Release 1.2 by Nils Bandener.")
  243.  
  244. call writeln("file", "\begin{picture}(" || w || "," || h || ")(0, 0) \linethickness{0.15mm}")
  245.  
  246. /*
  247.  * Draw arrows
  248.  */
  249.  
  250. call writeln("file", "\put(" || 0 || "," || xaxisy * yfact + yabs || "){\vector(1,0){" || w || "}}")
  251.  
  252. call writeln("file", "\put(" || yaxisx * xfact + xabs || "," || 0 || "){\vector(0,1){" || h || "}}")
  253.  
  254. /*
  255.  * Put field IDs at arrows
  256.  */
  257.  
  258. call writeln("file", "\put(" || w + 10 || ", " || xaxisy * yfact + yabs || "){ \makebox(0,0){" || xname || "}}")
  259.  
  260. call writeln("file", "\put(" || yaxisx * xfact + xabs || "," || h + 10 || "){ \makebox(0,0){" || yname || "}}")
  261.  
  262. /*
  263.  * Calculate best scaling for max-values
  264.  */
  265.  
  266. /*
  267.  * This emulates log base 5:
  268.  *
  269.  * b log x = ln x / ln b
  270.  */
  271.  
  272. ptx = int(ln(realw)/ln(scalebasex)-1)
  273. pty = int(ln(realh)/ln(scalebasey)-1)
  274.  
  275. contloop = 1
  276.  
  277. /*
  278.  *  calculate good values for scala
  279.  */
  280.  
  281. do while contloop
  282.  
  283.     xscale = pow(scalebasex,ptx)
  284.     yscale = pow(scalebasey,pty)
  285.  
  286.     xscalenum = trunc(realw / xscale * 1.05 + .5)
  287.     yscalenum = trunc(realh / yscale * 1.05 + .5)
  288.  
  289.     contloop = 0
  290.  
  291.     if xscalenum > 30 then do
  292.  
  293.         ptx = ptx + 1
  294.         contloop = 1
  295.  
  296.     end
  297.  
  298.     if yscalenum > 30 then do
  299.  
  300.         pty = pty + 1
  301.         contloop = 1
  302.  
  303.     end
  304. end
  305.  
  306. /*
  307.  *  Draw scala on x axis
  308.  */
  309.  
  310. startval = trunc(minx / xscale) * xscale
  311.  
  312. do forever
  313.  
  314.     coord = startval * xfact + xabs
  315.  
  316.     if coord < 0 then startval = startval + xscale / 2
  317.     else break
  318.  
  319. end
  320.  
  321. call writeln("file", "\multiput(" || coord || ", " || xaxisy * yfact - 1 + yabs || ")(" || xscale * xfact / 2 || ",0){" || xscalenum * 2 - 1 || "}{\line(0,1){2}}")
  322.  
  323. numbers = ""
  324.  
  325. places = -trunc(log10(xscale))
  326. if places < 0 then places = 0
  327.  
  328. curval = startval
  329.  
  330. do i = 1 to xscalenum
  331.  
  332.     if curval = 0 then
  333.         numbers = numbers || " "
  334.     else
  335.         numbers = numbers || trunc(curval, places)
  336.  
  337.     curval = curval + xscale
  338.  
  339.     if i ~= xscalenum then numbers = numbers || ", "
  340.  
  341. end
  342.  
  343. call writeln("file", "\multiputlist(" || coord || "," || xaxisy * yfact - 8 + yabs || ")(" || xscale * xfact || ",0){" || numbers || "}")
  344.  
  345. /*
  346.  *  Draw scala on y axis
  347.  */
  348.  
  349. startval = trunc(miny / yscale) * yscale
  350.  
  351. do forever
  352.  
  353.     coord = startval * yfact + yabs
  354.  
  355.     if coord < 0 then startval = startval + yscale / 2
  356.     else break
  357.  
  358. end
  359.  
  360. call writeln("file", "\multiput(" || yaxisx * xfact - 1 + xabs || ", " || coord || ")(0," || yscale * yfact / 2 || "){" || yscalenum * 2 - 1 || "}{\line(1,0){2}}")
  361.  
  362. numbers = ""
  363.  
  364. places = -trunc(log10(yscale))
  365. if places < 0 then places = 0
  366.  
  367. curval = startval
  368.  
  369. do i = 1 to yscalenum
  370.  
  371.     if curval = 0 then
  372.         numbers = numbers || " "
  373.     else
  374.         numbers = numbers || trunc(curval, places)
  375.  
  376.     curval = curval + yscale
  377.  
  378.     if i ~= yscalenum then numbers = numbers || ", "
  379.  
  380. end
  381.  
  382. call writeln("file", "\multiputlist(" || yaxisx * xfact - 12 + xabs || ", " || coord || ")(0," || yscale * yfact || "){" || numbers || "}")
  383.  
  384. /*
  385.  *  Draw graphic
  386.  */
  387.  
  388. call writeln("file", "\begin{drawjoin} \thinlines")
  389.  
  390. do i = 1 to num
  391.  
  392.     F_Progress i num
  393.  
  394.     F_GetFieldCont xname Record i
  395.     xvalue = result
  396.  
  397.     F_GetFieldCont yname Record i
  398.     yvalue = result
  399.  
  400.     call writeln("file", "\jput(" || xvalue * xfact + xabs || ", " || yvalue * yfact + yabs || "){ " || cross || "}")
  401.  
  402.     /*
  403.      * Put the field ID at the graph
  404.      *
  405.      * This would be useful for diagrams with several graphs,
  406.      * not used now
  407.      *
  408.  
  409.     if i = num then do
  410.  
  411.         call writeln("file", "\put(" || xvalue * xfact + 15 + xabs || ", " || yvalue * yfact + yabs || "){ " || xname || " }")
  412.  
  413.     end
  414.  
  415.      */
  416. end
  417.  
  418.  
  419. call writeln("file", "\end{drawjoin}")
  420.  
  421. call writeln("file", "\end{picture}")
  422.  
  423. if completefile = 1 then
  424.     call writeln("file", "\end{document}")
  425.  
  426. call close("file")
  427.  
  428. /*
  429.  *  If Print has been selected, do the job
  430.  */
  431.  
  432. if desttype = 0 then do
  433.  
  434.     F_SetStatus '"virtex..."'
  435.  
  436.     Address COMMAND
  437.  
  438.     'tex:bin/virtex &latex' '"' || texfile || '"'
  439.  
  440.     /*
  441.      *  Should check for errors here
  442.      */
  443.  
  444.     dotpos = lastpos(".", texfile)
  445.  
  446.     if dotpos ~= 0 then namebody = substr(texfile, 1, dotpos-1)
  447.     else namebody = texfile
  448.  
  449.     dvifile = namebody || ".dvi"
  450.  
  451.     finished = 0
  452.  
  453.     do while ~finished
  454.  
  455.         Address FIASCO
  456.         F_ResetStatus
  457.  
  458.         F_RequestChoice Title '"Menu"' '"Do you want to view the file, print it or quit?"' '"View|Print|Quit"'
  459.         choice = result
  460.  
  461.         if choice = 1 then do
  462.  
  463.             F_SetStatus '"showdvi..."'
  464.  
  465.             Address COMMAND
  466.  
  467.             'tex:bin/showdvi' '"' || DVIFile || '"'
  468.  
  469.         end
  470.         else if choice = 2 then do
  471.  
  472.             F_SetStatus '"dviprint..."'
  473.  
  474.             Address COMMAND
  475.  
  476.             'tex:bin/dviprint' '"' || DVIFile || '"'
  477.  
  478.         end
  479.         else finished = 1
  480.  
  481.     end
  482.  
  483.     call Delete(texfile)
  484.     call Delete(DVIFile)
  485.     call Delete(DVIFile || ".info")
  486.     call Delete(namebody || ".log")
  487.     call Delete(namebody || ".aux")
  488.  
  489. end
  490.  
  491. call bailout
  492.  
  493. /*
  494.  *  Procedures
  495.  */
  496.  
  497.  
  498. /*
  499.  *  bailout
  500.  *
  501.  *  Clean up and exit the script
  502.  */
  503.  
  504. bailout:
  505.  
  506. Address FIASCO
  507. F_ResetStatus
  508. F_UnlockGUI
  509.  
  510. exit
  511.  
  512. syntax:
  513. failure:
  514. say "Error" rc "in line" sigl ":" errortext(rc)
  515.  
  516. halt:
  517. break_c:
  518. say "Break"
  519. say "Enter to continue"
  520.  
  521. pull dummy
  522.  
  523. call bailout
  524.